<?php
/**
 * 
 * Cosa database file
 * 
 * @description		This class does not handle queries etc. Just use the standard php functions.
 * @author			Stijn Van Minnebruggen
 * @version			1.0.0
 * 
 */

class db
{
	/**
	 * Initialise class
	 * 
	 * @return	void
	 */
	
	public function __construct()
	{
		global $cosa;
		if(!defined('DB_HOST') || !defined('DB_USER') || !defined('DB_PASS') || !defined('DB_NAME')) $cosa->kill('Database settings not set. Please check your config file.', 'db');
		@mysql_connect(DB_HOST, DB_USER, DB_PASS) or $cosa->kill('Could not connect to database.<br />Please check config file or database.', 'db');
		@mysql_select_db(DB_NAME) or $cosa->kill('Database connection is ok, but could not select the database.<br />Make sure the database name has been set up correctly in the config file and that your database exists.', 'db');
	}
	
	
	/**
	 * Check if table exists
	 * 
	 * @param	string		$table_name
	 * @return	bool
	 */
	
	public function table_exists($table_name)
	{
		return mysql_query("DESC ".$table_name);
	}
	
	
	/**
	 * Add the database prefix for tables
	 * 
	 * @param	string		$table_name
	 * @return	string
	 */
	
	public function table($table_name)
	{
		return DB_PREFIX.$table_name;
	}
	
	
	/**
	 * Get the database table name for module tables
	 * 
	 * @param	string		$table_name
	 * @return	string
	 */
	
	public function module($table_name)
	{
		return DB_PREFIX.'mod_'.$table_name;
	}
	
	
	/**
	 * Get the database table name for plugin tables
	 * 
	 * @param	string		$table_name
	 * @return	string
	 */
	
	public function plugin($table_name)
	{
		return DB_PREFIX.'plug_'.$table_name;
	}
	
	
}


?>